home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap08 / WhatClr / WhatClr.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.0 KB  |  131 lines

  1. /*------------------------------------------
  2.    WHATCLR.C -- Displays Color Under Cursor
  3.                 (c) Charles Petzold, 1998
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER    1
  9.  
  10. void FindWindowSize (int *, int *) ;
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      static TCHAR szAppName[] = TEXT ("WhatClr") ;
  17.      HWND         hwnd ;
  18.      int          cxWindow, cyWindow ;
  19.      MSG          msg ;
  20.      WNDCLASS     wndclass ;
  21.      
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      
  33.      if (!RegisterClass (&wndclass))
  34.      {
  35.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  36.                       szAppName, MB_ICONERROR) ;
  37.           return 0 ;
  38.      }
  39.      
  40.      FindWindowSize (&cxWindow, &cyWindow) ;
  41.      
  42.      hwnd = CreateWindow (szAppName, TEXT ("What Color"),
  43.                           WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_BORDER,
  44.                           CW_USEDEFAULT, CW_USEDEFAULT,
  45.                           cxWindow, cyWindow,
  46.                           NULL, NULL, hInstance, NULL) ;
  47.      
  48.      ShowWindow (hwnd, iCmdShow) ;
  49.      UpdateWindow (hwnd) ;
  50.      
  51.      while (GetMessage (&msg, NULL, 0, 0))
  52.      {
  53.           TranslateMessage (&msg) ;
  54.           DispatchMessage (&msg) ;
  55.      }
  56.      return msg.wParam ;
  57. }
  58.  
  59. void FindWindowSize (int * pcxWindow, int * pcyWindow)
  60. {
  61.      HDC        hdcScreen ;
  62.      TEXTMETRIC tm ;
  63.      
  64.      hdcScreen = CreateIC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
  65.      GetTextMetrics (hdcScreen, &tm) ;
  66.      DeleteDC (hdcScreen) ;
  67.      
  68.      * pcxWindow = 2 * GetSystemMetrics (SM_CXBORDER)  + 
  69.                         12 * tm.tmAveCharWidth ;
  70.  
  71.      * pcyWindow = 2 * GetSystemMetrics (SM_CYBORDER)  +
  72.                        GetSystemMetrics (SM_CYCAPTION) + 
  73.                          2 * tm.tmHeight ;
  74. }
  75.  
  76. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  77. {
  78.      static COLORREF cr, crLast ;
  79.      static HDC      hdcScreen ;
  80.      HDC             hdc ;
  81.      PAINTSTRUCT     ps ;
  82.      POINT           pt ;
  83.      RECT            rc ;
  84.      TCHAR           szBuffer [16] ;
  85.      
  86.      switch (message)
  87.      {
  88.      case WM_CREATE:
  89.           hdcScreen = CreateDC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
  90.           SetTimer (hwnd, ID_TIMER, 100, NULL) ;
  91.           return 0 ;
  92.  
  93.      case WM_DISPLAYCHANGE:
  94.           DeleteDC (hdcScreen) ;
  95.           hdcScreen = CreateDC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
  96.           return 0 ;
  97.  
  98.      case WM_TIMER:
  99.           GetCursorPos (&pt) ;
  100.           cr = GetPixel (hdcScreen, pt.x, pt.y) ;
  101.           
  102.           if (cr != crLast)
  103.           {
  104.                crLast = cr ;
  105.                InvalidateRect (hwnd, NULL, FALSE) ;
  106.           }
  107.           return 0 ;
  108.           
  109.      case WM_PAINT:
  110.           hdc = BeginPaint (hwnd, &ps) ;
  111.           
  112.           GetClientRect (hwnd, &rc) ;
  113.           
  114.           wsprintf (szBuffer, TEXT ("  %02X %02X %02X  "),
  115.                     GetRValue (cr), GetGValue (cr), GetBValue (cr)) ;
  116.           
  117.           DrawText (hdc, szBuffer, -1, &rc,
  118.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
  119.           
  120.           EndPaint (hwnd, &ps) ;
  121.           return 0 ;
  122.           
  123.      case WM_DESTROY:
  124.           DeleteDC (hdcScreen) ;
  125.           KillTimer (hwnd, ID_TIMER) ;
  126.           PostQuitMessage (0) ;
  127.           return 0 ;
  128.      }
  129.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  130. }
  131.